//
//  DSScanvasView.m
//  Note
//
//  Created by DongSen on 2018/9/11.
//  Copyright © 2018年 董森森. All rights reserved.
//

#import "DSScanvasView.h"

@interface DSScanvasView ()

@property(nonatomic,strong)NSMutableArray * pointArray;

@end

@implementation DSScanvasView

@synthesize paths = _paths;

- (NSMutableArray *)pointArray{
    if (!_pointArray){
        _pointArray = [NSMutableArray array];
    }
    return _pointArray;
}
- (UIColor *)pathColor{
    if (!_pathColor){
        _pathColor = [UIColor blackColor];
    }
    return _pathColor;
}
- (void)clearAllPaths {
    [self.paths removeAllObjects];
    [self setNeedsDisplay];
}
- (void)back{
    if (self.paths.count){
        [self.backPaths addObject:self.paths.lastObject];
        [self.paths removeLastObject];
        [self setNeedsDisplay];
    }
}
- (void)forward{
    if (self.backPaths.count){
        [self.paths addObject:self.backPaths.firstObject];
        [self.backPaths removeObjectAtIndex:0];
        [self setNeedsDisplay];
    }
}
- (void)drawRect:(CGRect)rect{
    //绘制背景色
    [[UIColor whiteColor] setFill];
    UIRectFill(rect);
    if (self.block) {
        if (self.paths.count>0) {
            self.block(YES);
        }
        else{
            self.block(NO);
        }
    }
    // Drawing code
    for (NSDictionary * dic  in self.paths){
        UIBezierPath *path = [dic valueForKey:@"Path"];
        UIColor * color = [dic valueForKey:@"color"];
        [color setStroke];
        [path stroke];
    }
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches allObjects].lastObject;
    CGPoint startPoint = [touch locationInView:self];
    [self.bPath moveToPoint:startPoint];
    [self.pointArray addObject:NSStringFromCGPoint(startPoint)];
    NSDictionary * dic = @{@"color":self.pathColor,
                                  @"Path":self.bPath,
                           @"points":self.pointArray
                                  };
    [self.paths addObject:dic];
    [self.backPaths removeAllObjects];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch * touch = [touches allObjects].lastObject;
    CGPoint newPoint = [touch locationInView:self];
    [self.pointArray addObject:NSStringFromCGPoint(newPoint)];
    [self.bPath addLineToPoint:newPoint];
    [self setNeedsDisplay];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    self.bPath = nil;
    self.pointArray = nil;
}

- (UIBezierPath *)bPath{
    if (!_bPath) {
        _bPath = [UIBezierPath bezierPath];
        _bPath.lineJoinStyle = kCGLineJoinRound;
        _bPath.lineCapStyle = kCGLineCapRound;
        _bPath.lineWidth = 2.f;
    }
    return _bPath;
}

- (NSMutableArray *)paths{
    if (!_paths){
        _paths = [NSMutableArray array];
    }
    return _paths;
}
- (void)setPaths:(NSMutableArray *)paths{
     _paths = paths;
    [self layoutIfNeeded];
}
- (NSMutableArray *)backPaths{
    if (!_backPaths) {
        _backPaths = [NSMutableArray array];
    }
    return _backPaths;
}

- (NSUInteger)pathCount{
    return self.paths.count;
}

-(UIImage *)snapImage{
    UIGraphicsBeginImageContextWithOptions(self.bounds.size,YES,[UIScreen mainScreen].scale);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *snapImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return snapImage;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
